home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10558 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  58 lines

  1. Path: nntp-serv.cam.ac.uk!gdr11
  2. From: gdr11@cl.cam.ac.uk (Gareth Rees)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: variable-lenght argument list
  5. Date: 18 Mar 1996 18:46:55 GMT
  6. Organization: University of Cambridge, England
  7. Message-ID: <GDR11.96Mar18184655@cl.cam.ac.uk>
  8. References: <4icl0o$b3j@saturn.exodus.net>
  9. NNTP-Posting-Host: stint.cl.cam.ac.uk
  10. In-reply-to: Austin Ju's message of 15 Mar 1996 20:47:20 GMT
  11.  
  12. Austin Ju <jun@pixera.com> wrote:
  13. > Is there any way we may call a function with varable-length argument
  14. > list within another function also with varable-length argument list.
  15.  
  16. There is no way to do this in ANSI C; see question 15.12 in the
  17. comp.lang.c FAQ list (ftp://rtfm.mit.edu/usenet/comp.lang.c/C-FAQ-list).
  18.  
  19.   15.12: How can I write a function which takes a variable number of
  20.          arguments and passes them to some other function (which takes a
  21.          variable number of arguments)?
  22.  
  23.   A:     In general, you cannot.  Ideally, you should provide a version
  24.          of that other function which accepts a va_list pointer
  25.          (analogous to vfprintf(); see question 15.5 above).  If the
  26.          arguments must be passed directly as actual arguments, or if
  27.          you do not have the option of rewriting the second function to
  28.          accept a va_list (in other words, if the second, called
  29.          function must accept a variable number of arguments, not a
  30.          va_list), no portable solution is possible.  (The problem could
  31.          perhaps be solved by resorting to machine-specific assembly
  32.          language; see also question 15.13 below.)
  33.  
  34. I note that the GNU coding standards suggest the following dreadful
  35. hack, which would render your program unportable to many systems (and
  36. maybe even to your own system should you change compiler).  But these
  37. may not be important considerations.
  38.  
  39.   In certain cases, it is ok to pass integer and pointer arguments
  40.   indiscriminately to the same function, and use no prototype on any
  41.   system.  For example, many GNU programs have error-reporting functions
  42.   that pass their arguments along to `printf' and friends:
  43.  
  44.      error (s, a1, a2, a3)
  45.           char *s;
  46.           int a1, a2, a3;
  47.      {
  48.        fprintf (stderr, "error: ");
  49.        fprintf (stderr, s, a1, a2, a3);
  50.      }
  51.  
  52.   In practice, this works on all machines, and it is much simpler than
  53.   any "correct" alternative.  Be sure *not* to use a prototype for such
  54.   functions.
  55.  
  56. -- 
  57. Gareth Rees
  58.